<?php

/**
 * User model
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://monzee.wordpress.com/license New BSD
 * @version   $Id: Users.php 38 2009-02-18 17:12:39Z monzee $
 */
class Admin_Users
{
    protected $_users = array();

    public function __construct()
    {
        $this->_users = array(
            array(
                'id'       => 1,
                'username' => 'admin',
                'password' => md5('admin'),
                'email'    => 'admin@example.com',
            ),
            array(
                'id'       => 2,
                'username' => 'monzee',
                'password' => md5('password'),
                'email'    => 'monzee@gmail.com',
            ),
            array(
                'id'       => 3,
                'username' => 'anon',
                'password' => md5('anon'),
                'email'    => 'anon@yahoo.com',
            ),
            array(
                'id'       => 4,
                'username' => 'foo',
                'password' => md5('bar'),
                'email'    => 'foo@foobar.com',
            ),
            array(
                'id'       => 5,
                'username' => 'superman',
                'password' => md5('unknown'),
                'email'    => 'sup@man.com',
            ),
            array(
                'id'       => 6,
                'username' => 'john',
                'password' => md5('john123'),
                'email'    => 'john@doe.com',
            ),
            array(
                'id'       => 7,
                'username' => 'superadmin',
                'password' => md5('secret'),
                'email'    => 'super@min.com',
            ),
        );
    }

    public function setUsers($users)
    {
        $this->_users = $users;
    }

    /**
     * Returns the user with the specified id
     * 
     * @param int $id
     * @return array
     */
    public function getUserById($id)
    {
        foreach ($this->_users as $user) {
            if ($user['id'] == $id) {
                return $user;
            }
        }
        return null; // or throw an exception?
    }

    /**
     * Returns the user with the specified username
     *
     * @param int $username
     * @return array
     */
    public function getUserByUsername($username)
    {
        foreach ($this->_users as $user) {
            if ($user['username'] == $username) {
                return $user;
            }
        }
        return null; // or throw an exception?
    }

    /**
     * Get the roles of the specified user
     * 
     * @param array $user
     * @return array Array of roles
     */
    public function getUserRoles(array $user)
    {
        $userId = $user['id'];
        $userRoles = new Admin_UserRoles();
        $roles = $userRoles->getRolesByUserId($userId);

        $roleList = new Admin_Roles();
        foreach ($roles as &$roleId) {
            $roleId = $roleList->getRoleById($roleId);
        }
        
        return $roles;
    }

}
